/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","sysmanMain", "bizProto","btnServ"],
		function($,datagridServ, sysmanMain, bizProto,btnServ) {

			'use strict';

			var _bizProto,_moduleConfig,_buttonConfig,
				//锚记datagrid目标元素,grid按钮元素
				$dg,$list_btn,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,$edit_window_height=700,$edit_window_width=900,window_btn,
				
				//业务表单对象
				$form				
				
			var public_method = {				
				
				init:function(doc){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					
					_defaultConfig ={
						doc:null,
						rootId:null,				
						panelSelector:'div.well',
						subSystemCode:null,
						moduleCode:null,
						editWindow:null,
						btnSelector:'div.tb-list td.list-btn',
						initQueryParams:null
					}
					
					_defaultConfig = $.extend({},_defaultConfig,_options)
					
					_bizProto = new bizProto(_defaultConfig);			
					_bizProto.gridHeight -= 15
					
					if(_defaultConfig.editWindow){
						$edit_window = $('#' + _defaultConfig.editWindow);			
					}
					private_method.init(doc);					
				}
				
			}

			var private_method = {
				init : function(doc) {
					if (window.console) {
					console.info(_moduleConfig.fullBizcode + ' modules gets ready and been loaded.');
					}					
					
					_buttonConfig={
						"新增":private_method.add,
					    "编辑":private_method.edit,
						 "删除":private_method.del						 								
					}
					
					private_method.initPath();
					private_method.initElement(_bizProto.pageDoc)
					
				},
				
				//初始化路径
				initPath:function(){
					
				},				
				
				//初始化元素
				initElement:function(doc){
					
					
					$list_btn = $(doc).find(_defaultConfig.btnSelector)
					
					//加载列表按钮
					var _btnParam = {
						bizcode:_moduleConfig.fullBizcode,
						rootPath:_bizProto.rootPath,
						container:listTd,
						callback:private_method.bindBtnEvent}
					btnServ.generateListBtn(_btnParam);
			
					$dg = $(doc).find("table.dg-list");
					
					var _listParam = {
						height :_bizProto.gridHeight,
						url : _bizProto.queryListUrl,
						toolbar:$(listTd),
						title:_moduleConfig.name,
						width : 'auto'
					}
					//初始化查询条件判断
					if(_defaultConfig.initQueryParams){
						_listParam.queryParams=_defaultConfig.initQueryParams
					}
					$grid = $dg.datagrid(_listParam);									
					$.messager.progress('close');
				},
				
				//绑定按钮事件
				bindBtnEvent:function(){
					// 绑定按钮事件
					var btn = $(_bizProto.pageDoc).find(_defaultConfig.btnSelector +' a.function-btn')
					$(btn).unbind('click');
					$(btn).bind('click', private_method.function_btn_listenner)			
					
					
				},
				
				setComboValue:function(node,target){
		    		if(node!=null){		    			
		    			$(target).val(node.text);
		    		}
		    	},
				// 功能按钮监听函数
				function_btn_listenner : function(e) {

					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

				},
				// 新增函数
				add : function() {
					$($grid).datagrid('clearSelections')
					private_method.edit(false);
				},
				// 编辑,flag 为form按钮可见性标志
				edit : function(flag) {	
					console.info(_moduleConfig.fullBizcode+' edit on air ...');
					var _param = {grid:$grid,window:$edit_window,href:_bizProto.flowEidtPageUrl,width:$edit_window_width,height:$edit_window_height,flag:flag,existCheckElement:'form'}
					datagridServ.windowEdit(_param);					
				},
				
				
				// 删除
				del : function() {
					if (window.console) {
					console.info(_moduleConfig.fullBizcode+' delete on air ...');
					}
					datagridServ.delRow({url:_bizProto.delUrl,target:$grid});
				}

			};

			
			return public_method;

		});